package com.ssy.lingxi.contract.serviceimpl;

import com.ssy.lingxi.contract.entity.ContractParam;
import com.ssy.lingxi.contract.repository.ContractParamRepository;
import com.ssy.lingxi.contract.service.IContractDataInitService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 合同服务数据初始化
 * @author 万宁
 * @version 2.0.0
 * @date 2021-06-15
 */
@Service
public class ContractDataInitServiceImpl implements IContractDataInitService {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private ContractParamRepository contractParamRepository;

    /**
     * 初始化合同服务数据
     */
    @Override
    public void initContractServiceData() {
        //合同参数配置
        initContractParam();
    }

    /**
     * 初始化合同参数配置
     */
    private void initContractParam() {
        if(countTable("con_contract_param") > 0) {
            return;
        }

        alterSequence("con_contract_param_seq");

        List<ContractParam> paramList = new ArrayList<>();
        ContractParam param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("甲方(采购方)");
        param.setName("buyerName");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("甲方(供应方)");
        param.setName("supplierName");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("联系人(甲方)");
        param.setName("buyerContractName");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("联系电话(甲方)");
        param.setName("buyerContractMobile");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("收货地址(甲方)");
        param.setName("buyerReceiveAddress");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("开户行(乙方)");
        param.setName("supplierBank");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("开户名(乙方)");
        param.setName("supplierAccount");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("银行账号(乙方)");
        param.setName("supplierCardNo");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("代表人(甲方)");
        param.setName("buyerRepName");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("地址(甲方)");
        param.setName("buyerAddress");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("电话(甲方)");
        param.setName("buyerMobile");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("代表人(乙方)");
        param.setName("supplierRepName");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("地址(乙方)");
        param.setName("supplierAddress");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("电话(乙方)");
        param.setName("supplierMobile");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("商品信息");
        param.setName("goods");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("品牌");
        param.setName("brand");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("产品名称");
        param.setName("product");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("规格");
        param.setName("model");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("数量");
        param.setName("count");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("单价");
        param.setName("price");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("金额");
        param.setName("amount");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("合计");
        param.setName("total");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("备注");
        param.setName("remark");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("商品金额合计(大写)");
        param.setName("productAmountWord");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("运输费金额合计(大写)");
        param.setName("freightAmountWord");
        param.setState(1);
        paramList.add(param);

        param = new ContractParam();
        param.setCreateTime(System.currentTimeMillis());
        param.setDescription("订单总额合计(大写)");
        param.setName("totalAmountWord");
        param.setState(1);
        paramList.add(param);

        contractParamRepository.saveAll(paramList);
    }

    /**
     * 执行 select count(id) from table 语句
     * @param dbTableName 数据库的表名称
     * @return 表数据行数
     */
    private long countTable(String dbTableName) {
        String sql = "select count(id) as total from " + dbTableName + ";";
        SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet(sql);
        long total = 0;
        while (sqlRowSet.next()) {
            total = sqlRowSet.getLong("total");
        }
        return total;
    }

    /**
     * 更改序列值为1
     * @param sequenceName 序列名称
     */
    private void alterSequence(String sequenceName) {
        String sql = "alter sequence " + sequenceName + " restart with 1";
        jdbcTemplate.execute(sql);
    }
}
